package com.yuanian.timingwheel.redis;

import com.yuanian.timingwheel.context.SpringContext;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;
import java.util.Set;

public class RedisMap<T, V> {

    private String bigKey;

    private T smallKey;

    private V value;

    HashOperations<String, T, V> operations;

    public RedisMap(String bigKey) {
        this.bigKey =  "htliu-job:" + bigKey;
        this.operations = ((RedisTemplate)SpringContext.getBean("redisTemplate")).opsForHash();
    }

    public void clear() {
        Map map = this.operations.entries(this.bigKey);
        Set<Map.Entry> set = map.entrySet();
        for (Map.Entry entry : set) {
            this.operations.delete(this.bigKey, entry.getKey());
        }
    }

    public V get(T key) {
        V obj = this.operations.get(this.bigKey, key);
        //System.out.println("-----------" + obj);
        return obj;
    }

    public V put(T key, V value) {
        //System.out.println("putBucket:" + round + ":" + bucket);
        operations.put(this.bigKey, key, value);
        return value;
    }

    public void remove(T key) {
        operations.delete(this.bigKey, key);
    }

    public Map<T, V> map() {
        return operations.entries(this.bigKey);
    }

}
